package cn.zwt.wolf.service.content;

import cn.zwt.wolf.domain.content.Content;
import cn.zwt.wolf.repository.content.ContentRepository;
import cn.zwt.wolf.util.JPQLSearchException;
import cn.zwt.wolf.util.SearchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.NoResultException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangwt on 16-3-7.
 */
@Service
public class ContentService {
    @Autowired
    private ContentRepository contentRepository;
    @Autowired
    private SearchUtil searchUtil;

    private Logger logger = LoggerFactory.getLogger(ContentService.class);

    public List<Content> queryListForArrange (Map map){

        List<Content> contents = null;
        if (map==null){
            return contents;
        }
        StringBuilder hql =new StringBuilder("from Content where 1 = 1 ");
        Map<String,Object> params= new HashMap<String, Object>();
        if (map.containsKey("status")){
            hql.append(" and status =:status") ;
            params.put("status",Integer.valueOf(map.get("status").toString()));
        }
        if (map.containsKey("areaCode")){
            String[] areaCodes = map.get("areaCode").toString().split("\\|");
            if (areaCodes.length>0){
                for (String areaCode:areaCodes){
                    hql.append(" and areaCode like:");
                    hql.append(areaCode);
                    params.put(areaCode,"%"+areaCode+"%");
                }
            }
        }
        if (map.containsKey("languageCode")){
            String[] languageCodes = map.get("languageCode").toString().split("\\|");
            if (languageCodes.length>0){
                for (String languageCode:languageCodes){
                    hql.append(" and languageCode like:");
                    hql.append(languageCode);
                    params.put(languageCode,"%"+languageCode+"%");
                }
            }
        }
        if (map.containsKey("tagCode")){
            String[] tagCodes = map.get("tagCode").toString().split("\\|");
            if (tagCodes.length>0){
                for (String tagCode:tagCodes){
                    hql.append(" and tagsCode like:");
                    hql.append(tagCode);
                    params.put(tagCode,"%"+tagCode+"%");
                }
            }
        }
        if (map.containsKey("endScore")){
            hql.append(" and score <=:endScore") ;
            params.put("endScore",Float.valueOf(map.get("endScore").toString()));
        }
        if (map.containsKey("startScore")){
            hql.append(" and score >=:startScore") ;
            params.put("startScore",Float.valueOf(map.get("startScore").toString()));
        }
        if (map.containsKey("endYear")){
            hql.append(" and year <=:endYear") ;
            params.put("endYear",Integer.valueOf(map.get("endYear").toString()));
        }
        if (map.containsKey("startYear")){
            hql.append(" and year >=:startYear") ;
            params.put("startYear",Integer.valueOf(map.get("startYear").toString()));
        }
        if (map.containsKey("order")){
            hql.append(" order by "+map.get("order")+" desc ") ;
        }else {
            hql.append(" order by updateTime desc") ;
        }
        if (map.containsKey("length")) {
           hql.append(" limit "+Integer.valueOf(map.get("length").toString())+" ");
        }
        try {
            contents =  searchUtil.getResultList(hql.toString(), params);
        } catch (JPQLSearchException e) {
            logger.error("queryListForArrange error :" + e.getMessage());
        }catch (NoResultException e){
            logger.error("queryListForArrange error :" + e.getMessage());
        }

        return contents;
    }

    public Content findContentByContentId(int contentId){
        Content content = null;
        if ((Integer)contentId == null)
            return content;
        String hql = "from Content where status = 1 and contentId=:contentId";
        Map<String,Object> params= new HashMap<String, Object>();
        params.put("contentId",contentId);
        try {
            content = (Content) searchUtil.getSingleResult(hql, params);
        } catch (JPQLSearchException e) {
            logger.error("findContentByContentId error :" + e.getMessage());
        }catch (NoResultException e){
            logger.error("findContentByContentId error :" + e.getMessage());
        }
        return content;
    }

    public void saveOrUpdate(Content content){
        if (content!=null)
            contentRepository.save(content);
    }
}
